package topin.facecat.FaceCat.UI

import facecat.topin.core.FCColor
import facecat.topin.core.FCFont
import facecat.topin.core.FCPaint
import facecat.topin.core.FCRect
import facecat.topin.core.FCSize
import facecat.topin.core.FCTran


object FCDraw {
    fun drawText(
        paint: FCPaint,
        text: String?,
        dwPenColor: Long,
        font: FCFont?,
        x: Int,
        y: Int
    ): FCSize {
        val tSize = paint.textSize(text, font, -1)
        val tRect = FCRect(x, y, x + tSize.cx, y + tSize.cy)
        paint.drawText(text, dwPenColor, font, tRect, -1)
        return tSize
    }

    fun drawUnderLineNum(
        paint: FCPaint,
        value: Double,
        digit: Int,
        font: FCFont?,
        fontColor: Long,
        zeroAsEmpty: Boolean,
        x: Int,
        y: Int
    ): Int {
        return if (zeroAsEmpty && value == 0.0) {
            val text = "-"
            val size = paint.textSize(text, font, -1)
            drawText(paint, text, fontColor, font, x, y)
            size.cx
        } else {
            val nbs = FCTran.getValueByDigit(value, digit).split("[.]".toRegex())
                .dropLastWhile { it.isEmpty() }
                .toTypedArray()
            if (nbs.size == 1) {
                val size = paint.textSize(nbs[0], font, -1)
                drawText(paint, nbs[0], fontColor, font, x, y)
                size.cx
            } else {
                val decimalSize = paint.textSize(nbs[0] + ".", font, -1)
                val size = paint.textSize(nbs[1], font, -1)
                drawText(paint, nbs[0] + ".", fontColor, font, x, y)
                drawText(
                    paint, nbs[1], fontColor, font, x
                            + decimalSize.cx + 1, y
                )
                decimalSize.cx + size.cx
            }
        }
    }

    fun drawUnderLineNum2(
        paint: FCPaint,
        value: Double,
        digit: Int,
        font: FCFont?,
        fontColor: Long,
        zeroAsEmpty: Boolean,
        x: Int,
        y: Int
    ): Int {
        return if (zeroAsEmpty && value == 0.0) {
            0
        } else {
            val nbs = FCTran.getValueByDigit(value, digit).split("[.]".toRegex())
                .dropLastWhile { it.isEmpty() }
                .toTypedArray()
            if (nbs.size == 1) {
                val size = paint.textSize(nbs[0], font, -1)
                drawText(paint, nbs[0], fontColor, font, x, y)
                size.cx
            } else {
                val decimalSize = paint.textSize(nbs[0] + ".", font, -1)
                val size = paint.textSize(nbs[1], font, -1)
                drawText(paint, nbs[0] + ".", fontColor, font, x, y)
                drawText(
                    paint, nbs[1], fontColor, font, x
                            + decimalSize.cx + 1, y
                )
                decimalSize.cx + size.cx
            }
        }
    }

    fun getPriceColor(price: Double, comparePrice: Double): Long {
        if (price != 0.0) {
            if (price > comparePrice) {
                return FCColor.rgb(255, 82, 82)
            } else if (price < comparePrice) {
                return FCColor.rgb(46, 255, 50)
            }
        }
        return FCColor.rgb(190, 190, 235)
    }
}

